package com.zhoug.player.exoplayer.v2;

import android.util.SparseArray;

import androidx.annotation.Nullable;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.extractor.ts.DefaultTsPayloadReaderFactory;
import androidx.media3.extractor.ts.PesReader;
import androidx.media3.extractor.ts.TsPayloadReader;

/**
 * @author: zhoug
 * @since: 2025-07-23
 */
@UnstableApi 
public class FixedTsPayloadReaderFactory  implements TsPayloadReader.Factory  {
    private static final int AUDIO_STREAM_TYPE = 0x0F; // AAC音频

    private DefaultTsPayloadReaderFactory delegate;

    @Override
    public SparseArray<TsPayloadReader> createInitialPayloadReaders() {
        return delegate.createInitialPayloadReaders();
    }

    @Nullable
    @Override
    public TsPayloadReader createPayloadReader(int streamType, TsPayloadReader.EsInfo esInfo) {
        if (streamType == AUDIO_STREAM_TYPE) {
            // 对AAC流使用包装器
            return new PesReader(new FixedAdtsReader(true));
        }
        return delegate.createPayloadReader(streamType,esInfo);
    }

}
